package com.hzhr.iot.device.service;

import com.hzhr.iot.core.annotation.ApiImplicitInternalParams;
import com.hzhr.iot.core.constant.AccessModeEnum;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.device.constant.Device_ErrorsEnum;
import com.hzhr.iot.device.dao.CollectDeviceDao;
import com.hzhr.iot.device.dao.IotDeviceDao;
import com.hzhr.iot.device.entity.IotDeviceEntity;
import com.hzhr.iot.device.model.nbMeter.NbMeterGetModel;
import com.hzhr.iot.device.model.nbMeter.NbMeterPostModel;
import com.hzhr.iot.device.model.nbMeter_sell.NbMeter_SellGetModel;
import com.hzhr.iot.device.model.nbMeter_sell.NbMeter_SellPostModel;
import com.hzhr.iot.device.queryModel.custom.CollectDeviceCustomQueryModel;
import com.hzhr.iot.device.queryModel.custom.IotDeviceCustomQueryModel;
import com.hzhr.iot.device.service.base.BaseService;
import com.hzhr.iot.login.sdk.model.userCard.UserCardModel;
import com.hzhr.iot.user.sdk.model.company.CompanyModel;
import com.hzhr.iot.user.sdk.model.dept.DeptModel;
import com.hzhr.iot.user.sdk.service.User_CompanyService;
import com.hzhr.iot.user.sdk.service.User_DeptService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class NbMeter_SellService extends BaseService {
    @Autowired
    IotDeviceDao iotDeviceDao;
    @Autowired
    User_CompanyService user_companyService;
    @Autowired
    User_DeptService user_deptService;

    //region javadoc注释 : post
    /**
     * 销售NB表 <br/><br/>
     * 1. 获取NB表：查询登录用户所在公司下的NB表物联台账 <br/>
     * 2, 验证NB表是否具备销售条件：如果该NB表的物联网台账有所属的燃气公司，或则没有代码，则不具备销售条件 <br/>
     * 3. 验证购买公司的合法性：查询购买公司，如果购买公司不存在或者不是燃气公司，则不合法 <br/>
     * 4. 将NB物联表分配到购买公司的顶级部门下：修改NB表的物联台账，将其分配到购买公司的顶级部门下 <br/>
     * 5. 返回信息 <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/><br/>
     * @param body <br/><br/>
     * @throws ServiceException <br/>
     * NBMETER_SELLSERVICE_POST_201 : <br/>&nbsp&nbsp&nbsp&nbsp异常类型 : 业务流程异常<br/>&nbsp&nbsp&nbsp&nbsp异常业务 : 获取NB表<br/>&nbsp&nbsp&nbsp&nbsp异常描述 : NB表不存在<br/>
     * NBMETER_SELLSERVICE_POST_202 : <br/>&nbsp&nbsp&nbsp&nbsp异常类型 : 业务流程异常<br/>&nbsp&nbsp&nbsp&nbsp异常业务 : 验证购买公司的合法性<br/>&nbsp&nbsp&nbsp&nbsp异常描述 : 购买公司不合法<br/>
     * NBMETER_SELLSERVICE_POST_203 : <br/>&nbsp&nbsp&nbsp&nbsp异常类型 : 业务流程异常<br/>&nbsp&nbsp&nbsp&nbsp异常业务 : 验证NB表是否具备销售条件<br/>&nbsp&nbsp&nbsp&nbsp异常描述 : NB表已经销售，无法继续销售<br/>
     * NBMETER_SELLSERVICE_POST_204 : <br/>&nbsp&nbsp&nbsp&nbsp异常类型 : 业务流程异常<br/>&nbsp&nbsp&nbsp&nbsp异常业务 : 验证NB表是否具备销售条件<br/>&nbsp&nbsp&nbsp&nbsp异常描述 : NB表没有代码，无法销售<br/>
     * @see NbMeter_SellPostModel body <br/>
     * @see NbMeter_SellGetModel result <br/>
     */ 
    //endregion
    //region 说明 : post
    //销售NB表
    //1. 获取NB表：查询登录用户所在公司下的NB表物联台账
    //2, 验证NB表是否具备销售条件：如果该NB表的物联网台账有所属的燃气公司，或则没有代码，则不具备销售条件
    //3. 验证购买公司的合法性：查询购买公司，如果购买公司不存在或者不是燃气公司，则不合法
    //4. 将NB物联表分配到购买公司的顶级部门下：修改NB表的物联台账，将其分配到购买公司的顶级部门下
    //5. 返回信息
    //endregion
    //region 参数 : post
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),
    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),
    })
    //endregion
    //region 请求 : post
    @RequestMapping(value = "/nb-meters/{nbMeterId}/sell", method = RequestMethod.POST)
    //endregion
    @Transactional(rollbackFor = Exception.class)
    public NbMeter_SellGetModel post(Map<String, String> header, Integer nbMeterId,
                                     Map<String, Object> query, NbMeter_SellPostModel body) throws ServiceException {
        //获取NB表：查询登录用户所在公司下的NB表物联台账
        UserCardModel userCard = getUserCard(header);
        IotDeviceCustomQueryModel queryModel = new IotDeviceCustomQueryModel();
        queryModel.setEqual_companyId(userCard.getCompanyId());
        queryModel.setEqual_id(nbMeterId);
        queryModel.setShow(true);
        queryModel.setSet_isValid("1");
        queryModel.setEqual_categoryCode("1");
        IotDeviceEntity nbMeter = iotDeviceDao.selectEntity(queryModel);
        if (nbMeter == null) throw new ServiceException(Device_ErrorsEnum.NBMETER_SELLSERVICE_POST_201);

        //验证NB表是否具备销售条件：如果该NB表的物联网台账有所属的燃气公司，或则没有代码，则不具备销售条件
        if (nbMeter.getGasCompanyId() != null) throw new ServiceException(Device_ErrorsEnum.NBMETER_SELLSERVICE_POST_203);
        if (nbMeter.getCode() == null) throw new ServiceException(Device_ErrorsEnum.NBMETER_SELLSERVICE_POST_204);


        //验证购买公司的合法性：查询购买公司，如果购买公司不存在或者不是燃气公司，则不合法
        Integer purchasingCompanyId = body.getPurchasingCompanyId();
        Map<String, Object> query1 = new HashMap<>();
        query1.put("_equal_id", purchasingCompanyId);
        query1.put("_equal_type", "1");
        query1.put("show", "one");
        query1.put("show_info", true);
        CompanyModel company = user_companyService.get(initHeader(header, AccessModeEnum.GLOBAL), query1).getModel();
        if (company == null) throw new ServiceException(Device_ErrorsEnum.NBMETER_SELLSERVICE_POST_202);

        //将NB物联表分配到购买公司的顶级部门下：修改NB表的物联台账，将其分配到购买公司的顶级部门下
        Map<String, Object> query2 = new HashMap<>();
        query2.put("_equal_companyId", company.getId());
        query2.put("_equal_level", 1);
        query2.put("show", "one");
        query2.put("show_info", true);
        DeptModel dept = user_deptService.get(initHeader(header, AccessModeEnum.GLOBAL), query2).getModel();
        nbMeter.setGasCompanyId(company.getId());
        nbMeter.setDeptId(dept.getId());
        nbMeter.setResourceCode(dept.getResourceCode());
        nbMeter.setUpdateUserId(userCard.getUserId());
        nbMeter.setUpdateTime(new Date());
        nbMeter.setSaleTime(new Date());
        iotDeviceDao.update(nbMeter);

        //返回信息
        NbMeter_SellGetModel result = new NbMeter_SellGetModel();
        return result;
    }
}
